package cn.gdk.gaodekecc.service.impl;

import cn.gdk.gaodekecc.domain.Product;
import cn.gdk.gaodekecc.domain.PurchaseApply;
import cn.gdk.gaodekecc.domain.PurchaseRecord;
import cn.gdk.gaodekecc.domain.Stocks;
import cn.gdk.gaodekecc.feig.IProductService;
import cn.gdk.gaodekecc.feig.StocksService;
import cn.gdk.gaodekecc.mapper.PurchaseApplyMapper;
import cn.gdk.gaodekecc.mapper.PurchaseRecordMapper;
import cn.gdk.gaodekecc.result.JSONResult;
import cn.gdk.gaodekecc.service.IPurchaseApplyService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Value;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author director
 * @since 2022-11-20
 */
@Service
public class PurchaseApplyServiceImpl extends ServiceImpl<PurchaseApplyMapper, PurchaseApply> implements IPurchaseApplyService {
    @Value("${purchase_quantity}")
    private Integer purchaseQuantity;

    @Autowired
    private PurchaseApplyMapper purchaseApplyMapper;

    @Autowired
    private PurchaseRecordMapper purchaseRecordMapper;

    @Autowired
    private StocksService stocksService;

    @Autowired
    private IProductService productService;



    @Override
    public void purchaseApply(PurchaseApply purchaseApply) {
        purchaseApply.getProductId();
        JSONResult jsonResult = productService.get(Long.valueOf(purchaseApply.getProductId()));


        if(jsonResult!=null){
            Product product = (Product) jsonResult.getData();
            if (product!=null){
                if (purchaseApply.getCount() == null) {
                    purchaseApply.setCount(purchaseQuantity);
                }
                purchaseApply.setProductId(product.getId());
                purchaseApply.setProductName(product.getProductName());
                purchaseApply.setCreateTime(new Date());
                purchaseApply.setUpdateTime(new Date());
                purchaseApply.setState(0);
                purchaseApplyMapper.insert(purchaseApply);
            }
        }

    }
    @Override
    public List<PurchaseApply> purchaseApplyQuery(Integer state) {


        return purchaseApplyMapper.purchaseApplyQuery(state);
    }

    @Override
    public void purchaseApplyAudit(Integer id) {

        PurchaseApply purchaseApply = purchaseApplyMapper.selectById(id);
        JSONResult jsonResult = stocksService.get(Long.valueOf(purchaseApply.getProductId()));


        if (null==purchaseApply) {
            if (purchaseApply.getState()==0){
                if (id==-1){
                    purchaseApply.setState(-1);
                }else if (id==1){
                    purchaseApply.setState(1);

                    PurchaseRecord purchaseRecord = new PurchaseRecord();
                    purchaseRecord.setProductId(purchaseApply.getProductId());

                    purchaseRecord.setCount(purchaseApply.getCount());
                    purchaseRecord.setCreateTime(new Date());
                    purchaseRecord.setUpdateTime(new Date());
                    purchaseRecordMapper.insert(purchaseRecord);
                    Stocks stocks = (Stocks) jsonResult.getData();
                    stocks.setCount(purchaseApply.getCount() + stocks.getCount());
                    stocksService.warehousing(stocks);
                }
                purchaseApply.setUpdateTime(new Date());
                UUID uuid = UUID.randomUUID();
                purchaseApply.setApprovalId(Integer.valueOf(String.valueOf(uuid)));
                purchaseApplyMapper.updateById(purchaseApply);
            }

        }

       // throw new BusinessException("没有数据");
    }


}
